package com.sloan.community.util;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author: yaosj
 * @CreateTime: 2024-12-11
 */
public class TimeUtils {

    final static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd");

    public static String timeDiff(LocalDateTime givenTime) {
        Duration duration = Duration.between(givenTime, LocalDateTime.now());
        long seconds = duration.getSeconds();

        if (seconds < 60) {
            return "刚刚";
        } else if (seconds < 3600) {
            long minutes = duration.toMinutes();
            return minutes + "分钟前";
        } else if (seconds < 86400) {
            long hours = duration.toHours();
            return hours + "小时前";
        } else if (seconds < 2592000) {
            long days = duration.toDays();
            return days + "天前";
        } else {
            return givenTime.format(FORMATTER);
        }
    }

}
